package com.tea.room.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tea.room.dao.FaceDao;
import com.tea.room.model.FaceTable;
import com.tea.room.model.Page;
import com.tea.room.model.Return;
import com.tea.room.service.login.LoginService;
import com.tea.room.service.login.impl.LoginServiceImpl;

@RestController("loginController")
public class LoginController {

	@Autowired
	private LoginService loginService;
	
   @Autowired
    private FaceDao faceDao;

	private static Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);

	/**
	 * 用户登录接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/adminLogin")
	public Return adminLogin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String ip = request.getRemoteAddr();
		Return rn = loginService.findUser(username, password, ip);
		return rn;
	}

	/**
	 * 修改帳戶信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/mdfUser")
	public Return adminMdfInfo(HttpServletRequest request,
			HttpServletResponse response) {

		return null;
	}

	/**
	 * 帳戶註冊
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addUser")
	public Return addUser(HttpServletRequest request,
			HttpServletResponse response) {
		return null;
	}

	/**
	 * 查询用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryUser")
	public Page queryUser(Integer page, Integer rows) {
		logger.info("page:" + page);
		logger.info("rows:" + rows);
		Page user = loginService.findAllUser(page, rows);
		return user;
	}

	/**
	 * 查询用户
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/saveMsg")
	public Return saveMsg(HttpServletRequest request,
			HttpServletResponse response) {
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String amount = request.getParameter("amount");
		String price = request.getParameter("price");
		String message = request.getParameter("message");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String area = request.getParameter("area");
		String addressdetail = request.getParameter("addressdetail");
		FaceTable table = new FaceTable();
		table.setName(name);
		table.setAmount(amount);
		table.setAdderss(province + city + area + addressdetail);
		table.setPhone(phone);
		table.setPrice(price);
		table.setMessage(message);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		table.setCrt_date(format.format(new Date()));
		faceDao.insert(table);

		Return rn = new Return();
		rn.setIsSuccessflag(true);
		return rn;
	}
	
	
	@ResponseBody
    @RequestMapping(value = "/getmsg")
	public Return getmsg(HttpServletRequest request, HttpServletResponse response) {
	    List<FaceTable> queryList = faceDao.queryList();
	    Return rn = new Return();
        rn.setIsSuccessflag(true);
        rn.setDataList(queryList);
        return rn;
	}
	
}
